
#include "json/json.h"
#include "macro.h"
#include "tojson.h"
#include "pc.h"
#include "map.h"

//所有Json函数,返回序列化后的json;皆有两个参数: 1.所属类型的对象,2.序列化和反序列化的标志,



//client
//LoginInfo
string LoginInfo::Json(LoginInfo& logininfo, char* buff, int identifier)
{
    string done="Done!";
    if(identifier == DESERIALIZE)
    {
        string ss(buff);
        //cout<<"ss"<<ss<<endl;
        string strValue = ss;
        Json::Reader reader;
        Json::Value value;
        if (reader.parse(strValue, value))// reader将Json字符串解析到root，root将包含Json里所有子元素
        {
            bool ic= value["IsConnected"].asBool();  // 访问节点  并转换为bool类型
            string rm= value["Remark"].asString();
            int  ncn= value["NowConnectedNum"].asInt();
            int wpn= value["WaitingPeopNum"].asInt();
            cout<<"in: "<<ic<<" "<<rm<<" "<<ncn<<" "<<wpn<<endl;
            logininfo.IsConnected=ic;
            logininfo.Remark=rm;
            logininfo.NowConnectedNum=ncn;
            logininfo.WaitingPeopNum=wpn;
        }
    }
    else if(identifier == SERIALIZE)
    {
        //序列化
        Json::Value root;
        Json::Value arrayObj;
        Json::Value item;
        item["IsConnected"] = (logininfo.IsConnected)==1?"true":"false";
        item["Remark"] = logininfo.Remark;
        item["NowConnectedNum"] = logininfo.NowConnectedNum;
        item["WaitingPeopNum"] = logininfo.WaitingPeopNum;
        arrayObj.append(item);

        root=arrayObj;      //设置根

        //root.toStyledString();  //格式化输出json
        string out; //= root.toStyledString();
        //cout << out << endl;
        Json::FastWriter writer;
        out=writer.write(root);
        //cout << out << endl;
        int shit=0;
        char l='[';
        char r=']';
        //cout<<endl;
        while( shit < out.length())
        {
            //cout<<buff[shit];
            if((out[shit] == l) || (out[shit] == r))
            {
                out.erase(shit,1);
                shit--;
            }
            shit++;
        }
        cout<<buff<<endl;
        return out;
    }
    return done;
}


//server
//Player
string Player::Json(Player& player, char buff[4096], int& identifier)
{

}

//MAP
string Map::Json(Map& map, char buff[4096], int& identifier)
{

}


//Broadcastinfo
string BroadcastInfo::Json(BroadcastInfo &broadcastinfo, char *buff, int identifier)
{
    string done="Done!";
    //广播只有序列化
    if(identifier == SERIALIZE || identifier == DESERIALIZE)
    {
        //序列化
        Json::Value root;
        Json::Value arrayObj;
        Json::Value item;
        time_t rawtime;
        tm* timeinfo;
        time(&rawtime);
        timeinfo=localtime(&rawtime);
        strftime(buff,17,"%Y-%m-%d %I:%M",timeinfo);
        broadcastinfo.Time=buff;
        item["Time"] = broadcastinfo.Time;
        item["Content"] = broadcastinfo.Content;
        item["Remark"] = broadcastinfo.Remark;
        arrayObj.append(item);

        root=arrayObj;      //设置根

        //root.toStyledString();  //格式化输出json
        string out; //= root.toStyledString();
        //cout << out << endl;
        Json::FastWriter writer;
        out=writer.write(root);
        //cout << out << endl;
        int shit=0;
        char l='[';
        char r=']';
        cout<<endl;
        while( shit < out.length())
        {
            //cout<<buff[shit];
            if((out[shit] == l) || (out[shit] == r))
            {
                out.erase(shit,1);
                shit--;
            }
            shit++;
        }
        //cout<<buff<<endl;
        return out;
    }
    return done;
}

















